const express = require("express");
const router = express.Router();
const {success, successWithData} = require("../utils/result")
const { check } = require('express-validator');
const {checkParam}  = require("../utils/checkUtils")
const Member = require("../db/model/memberModel");
const {Op} = require("sequelize");


router.get("/checkHealth", function(req, res) {
  console.log("健康检查");
  return res.json(success());
})

/**
 * 分页查询接口
 * @api {POST} /member/queryMemberListByPage 分页查询接口
 * @apiDescription 分页查询接口
 * @apiName queryMemberListByPage
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} page=1   页码
 * @apiBody {Number} limit=10   每页条数
 * @apiParamExample {json} Request-Example:
 *     {
 *        "page": 1,
 *        "limit": "10",
 *     }
 * @apiGroup Member
 * @apiVersion 1.0.0
 */
router.post("/member/queryMemberListByPage", check('page').not().isEmpty(),check('limit').not().isEmpty(), async function (req, res) {
  checkParam(req)
  const { page, limit, type, nick_name } = req.body;
  const where = {};
  let offset = (page - 1) * limit;
  if (type) {
    where["type"] = type;
  }
  if (nick_name) {
    where["nick_name"] = {
      [Op.like]: "%" + nick_name + "%",
    };
  }
  const result = await Member.findAndCountAll({
    limit,
    offset,
    where,
    order: [["created_time", "DESC"]],
  });
  return res.json(successWithData(result));
});


/**
 * @api {POST} /member/add 添加
 * @apiDescription 添加
 * @apiName add
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   主键id
 * @apiBody {String} nick_name='会员昵称'   会员昵称
 * @apiBody {String} avatar='会员头像'   会员头像
 * @apiBody {String} mobile='会员手机号'   会员手机号
 * @apiBody {Number} status=1   状态：0 禁用 1 启用
 * @apiBody {String} email='会员邮箱'   会员邮箱
 * @apiBody {String} password='密码'   密码
 * @apiBody {Number} level_id=1   会员等级id
 * @apiBody {String} wx_token='微信token'   微信token
 * @apiBody {String} created_time='1994-04-01'   创建时间
 * @apiBody {String} updated_time='1994-04-01'   更新时间
 * @apiBody {String} last_login_time='1994-04-01'   最后登录时间
 * @apiParamExample {json} Request-Example:
 *     {
 *        "id": 1,
 *        "nick_name": '会员昵称',
 *        "avatar": '会员头像',
 *        "mobile": '会员手机号',
 *        "status": 1,
 *        "email": '会员邮箱',
 *        "password": '密码',
 *        "level_id": 1,
 *        "wx_token": '微信token',
 *        "created_time": '1994-04-01',
 *        "updated_time": '1994-04-01',
 *        "last_login_time": '1994-04-01',
 *     }
 * @apiGroup Member
 * @apiVersion 1.0.0
 */
router.post("/member/add",  async function (req, res) {
  await Member.create({...req.body});
  return res.json(success());
});


/**
 * @api {POST} /member/edit 编辑
 * @apiDescription 编辑
 * @apiName edit
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   主键id
 * @apiBody {String} nick_name='会员昵称'   会员昵称
 * @apiBody {String} avatar='会员头像'   会员头像
 * @apiBody {String} mobile='会员手机号'   会员手机号
 * @apiBody {Number} status=1   状态：0 禁用 1 启用
 * @apiBody {String} email='会员邮箱'   会员邮箱
 * @apiBody {String} password='密码'   密码
 * @apiBody {Number} level_id=1   会员等级id
 * @apiBody {String} wx_token='微信token'   微信token
 * @apiBody {String} created_time='1994-04-01'   创建时间
 * @apiBody {String} updated_time='1994-04-01'   更新时间
 * @apiBody {String} last_login_time='1994-04-01'   最后登录时间
 * @apiParamExample {json} Request-Example:
 *     {
 *        "nick_name": '会员昵称',
 *        "avatar": '会员头像',
 *        "mobile": '会员手机号',
 *        "status": 1,
 *        "email": '会员邮箱',
 *        "password": '密码',
 *        "level_id": 1,
 *        "wx_token": '微信token',
 *        "created_time": '1994-04-01',
 *        "updated_time": '1994-04-01',
 *        "last_login_time": '1994-04-01',
 *     }
 * @apiGroup Member
 * @apiVersion 1.0.0
 */
router.post("/member/edit", check('id').not().isEmpty(),async function (req, res) {
  checkParam(req)
  await Member.update({...req.body}, {where: {id:req.body.id}});
  return res.json(success());
});

/**
 * @api {POST} /member/del 删除
 * @apiDescription 删除
 * @apiName del
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   主键id
 * @apiParamExample {json} Request-Example:
 *     {
 *        "id": 1,
 *     }
 * @apiGroup Member
 * @apiVersion 1.0.0
 */
router.post("/member/del", check('id').not().isEmpty(),async function (req, res) {
  checkParam(req)
  await Member.destroy({where: {id:req.body.id}});
  return res.json(success());
});
module.exports = router;